// 公司管理

var constant = require('../../const');
var models = require('../../models');
var fs = require('fs');
var AV = require('leanengine');
var multiparty = require('multiparty');


function writeAritcleIndex(req, res) {
	if (!req.AV.user) {
		res.redirect('/admin');
	}
	res.render('admin/wxArticle/write', {
		"username": req.session.username,
		"avatar": req.session.useravatar
	});
}


function addArticle(req, res) {
	if (!req.AV.user) {
		res.redirect('/admin');
	}

	var form = new multiparty.Form();
	form.parse(req, function(err, fields, files) {
		var articleImg = files.articleImg[0];
		var title = fields.title.toString();
		var intro = fields.intro.toString();
		var address = fields.address.toString();

		if (articleImg.size !== 0) {
			fs.readFile(articleImg.path, function(err, data) {
				if (err) {
					var data = {
						"username": req.session.username,
						"errMsg": "图片保存失败, 请重试",
					};
					res.render('/admin/weixin/article/add', data);
				}
				var base64Data = data.toString('base64');
				var theFile = new AV.File(articleImg.originalFilename, {
					base64: base64Data
				});
				theFile.save().then(function(theFile) {
					// 文件保存成功, 新增服务
					var article = new models.wxArticle;
					article.set("intro", intro);
					article.set("addr", address);
					article.set("title", title);
					article.set("img", theFile);
					article.set("status", 0);
					article.save({
						success: function() {
							res.send({
								"code": 0,
								"msg": "成功"
							});
						},
						error: function(server, error) {
							console.log(error);
							//res.redirect('/admin/server');
						}
					});
				});
			});
		} else {
			var data = {
				"username": req.session.username,
				"errMsg": "请选择文件",
			};
			res.render('/admin/weixin/article/add', data);
		}
	})
}

function getSimpleArticle(req, res) {
	var id = req.query.id;
	var articlequery = new AV.Query(models.homeArticle);
	articlequery.get(id, {
		success: function(article) {
			var data = {
				"article": article
			}
			res.render('admin/homeArticle/homeArticleView', {
				data: data
			});
		},
		error: function(error) {
			//res.success(error);
		}
	})
}

function getArticle(req, res) {
	if (!req.AV.user) {
		res.redirect('/admin');
	}

	// 0是正在编辑的, 1 已经发布的, 2 已经禁用了的
	//	var status = req.query.status || 0;
	var query = new AV.Query(models.wxArticle);

	query.find({
		success: function(articles) {
			var data = {
				"username": req.session.username,
				"avatar": req.session.useravatar,
				"articles": articles
			};
			res.render('admin/wxArticle/index', data);
		},
		error: function(error) {
			res.render('admin/wxArticle/index', {
				"username": req.session.username,
				"avatar": req.session.useravatar,
			});
		}
	});

}

function editArticleIndex(req, res) {
	if (!req.AV.user) {
		res.redirect('/admin');
	}
	var id = req.query.id;
	var articlequery = new AV.Query(models.wxArticle);
	articlequery.get(id, {
		success: function(article) {
			var data = {
				"article": article,
				"username": req.session.username
			}
			res.render('admin/wxArticle/write', data);
		},
		error: function(error) {
			//res.success(error);
		}
	})
}

function editArticle(req, res) {
	if (!req.AV.user) {
		res.redirect('/admin');
	}

	var form = new multiparty.Form();
	form.parse(req, function(err, fields, files) {
		var articleImg = files.articleImg[0];
		var title = fields.title.toString();
		var intro = fields.intro.toString();
		var address = fields.address.toString();
		var articleId = fields.articleId;

		if (articleImg.size !== 0) {
			fs.readFile(articleImg.path, function(err, data) {
				if (err) {
					var data = {
						"username": req.session.username,
						"errMsg": "图片保存失败, 请重试",
					};
					res.render('/admin/weixin/article/add', data);
				}
				var base64Data = data.toString('base64');
				var theFile = new AV.File(articleImg.originalFilename, {
					base64: base64Data
				});
				theFile.save().then(function(theFile) {
					// 文件保存成功, 新增服务
					var article = new models.wxArticle;
					article.set("objectId", articleId);
					article.set("intro", intro);
					article.set("title", title);
					article.set("img", theFile);
					article.set('addr', address)
					article.set("status", 0);

					article.save({
						success: function() {
							res.send({
								"code": 0,
								"msg": "成功"
							});
						},
						error: function(server, error) {
							console.log(error);
							//res.redirect('/admin/server');
						}
					});
				});
			});
		} else {
			// 文件保存成功, 新增服务
			var article = new models.wxArticle;
			article.set("objectId", articleId);
			article.set("intro", intro);
			article.set("title", title);
			article.set('addr', address)
			article.set("status", 0);

			article.save({
				success: function() {
					res.send({
						"code": 0,
						"msg": "成功"
					});
				},
				error: function(server, error) {
					console.log(error);
				}
			});
		}
	})
}

function getWxArticle(req, res) {
	// 0是正在编辑的, 1 已经发布的, 2 已经禁用了的
	//	var status = req.query.status || 0;
	var query = new AV.Query(models.wxArticle);
	query.addDescending('createdAt');
	query.limit(1000);
	query.find({
		success: function(articles) {
			var data = {
				"articles": articles
			};
			res.render('admin/wxArticle/wxArticle', data);
		},
		error: function(error) {}
	});

}

function getWxWap(req, res) {
	var addr = req.query.addr;
	var data = {
		"addr": addr
	};
	res.render('admin/wxArticle/wxWrap', data);

}


exports.writeAritcleIndex = writeAritcleIndex;
exports.addArticle = addArticle;
exports.getSimpleArticle = getSimpleArticle;
exports.getArticle = getArticle;
exports.editArticleIndex = editArticleIndex;
exports.editArticle = editArticle;
exports.getWxArticle = getWxArticle;
exports.getWxWap = getWxWap;